package org.example.platform.elasticsearch.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import lombok.extern.slf4j.Slf4j;
import org.example.platform.elasticsearch.entity.Hotel;
import org.example.platform.elasticsearch.entity.HotelDocRequest;
import org.example.platform.elasticsearch.service.EsCreateDocService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class ESCreateController {
    @Autowired
    EsCreateDocService esCreateDocService;


    @PostMapping("/create/doc")
    public Map<String, Object> getResult(@RequestBody Hotel hotel) {
        if (hotel == null || CharSequenceUtil.isBlank(hotel.getIndex())) {
            Hotel hotel2 = new Hotel();
            hotel2.setTitle("参数错误");
            return (Map<String, Object>) new HashMap<>().put(100,hotel2);
        }
        HashMap<String, Object> dataMap = new HashMap<>();
        dataMap.put("title", hotel.getTitle());
        dataMap.put("city", hotel.getCity());
        dataMap.put("price", hotel.getPrice());
        try {
            Map<String, Object> resultMap = esCreateDocService.singleIndexDoc(dataMap, hotel.getIndex(), hotel.getId());
            return resultMap;
        } catch (IOException e) {
            Hotel hotel2 = new Hotel();
            hotel2.setTitle(e.getMessage());
            log.warn("创建文档发生异常，原因为:{}", e.getMessage());
            return (Map<String, Object>) new HashMap<>().put(100,hotel2);
        } catch (Exception e) {
            Hotel hotel2 = new Hotel();
            hotel2.setTitle(e.getMessage());
            log.error("服务发生异常，原因为:{}", e.getMessage());
            return (Map<String, Object>) new HashMap<>().put(100,hotel2);
        }
    }

    @PostMapping("/bulk/create/doc")
    public String bulkIndexDoc(@RequestBody HotelDocRequest hotelDocRequest) throws Exception {
        List<Hotel> hotelList = hotelDocRequest.getHotelList();
        if(CollUtil.isEmpty(hotelList)){
            return "无可插入的有效文档";
        }
        // json输入list<map<k,v>>结构复杂，后端做转化，前台只需输入list<hotel>的json
        ArrayList<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
        hotelList.forEach(hotel -> {
            HashMap<String,Object> dataMap = new HashMap<>();
            // 对比之前的插入单条文档，需要多加入一个id
            dataMap.put("id",hotel.getId());
            dataMap.put("title",hotel.getTitle());
            dataMap.put("city",hotel.getCity());
            dataMap.put("price",hotel.getPrice());
            list.add(dataMap);
        });
        hotelDocRequest.setRecordMapList(list);
        String s = esCreateDocService.bulkIndexDoc(hotelDocRequest);
        return s;
    }
}
